// JavaScript Document
var p = new Player();

//Camera
camera = {
	scale: 1,
	x: 0,
	y: 0
}

//Level
var mapFiles = new Array();
level = {
	name: "Error 100 - Level not found",
	map:[,],
	spawnPos: {
		"x":0,
 		"y":0
 	}
}

function initListeners() {
	
}

function update() {
	p.update();
	//updateArrowKeys();
	updateKeys();
	updateExercises();
	updateCamera();
}

function updateCamera() {
	ctx.setTransform(1, 0, 0, 1, 0, 0);
	camera.x = (-p.x - grid / 2) * camera.scale + canvas.width/2;
	camera.y = (-p.y - grid / 2) * camera.scale + canvas.height/2;
	ctx.translate(
		camera.x,
		camera.y);
	ctx.scale(camera.scale, camera.scale);
}

function updateExercises() {
	if (isPlaceFree(p.getPosX(), p.getPosY() - 1)) { //up
		if (exercises[0] == undefined) {
			do {
			exercises[0] = new Exercise(lvl);
			} while (answerOverlapping(0));
		}
	} else {
		if (exercises[0] !== undefined) {
			exercises[0] = undefined;
		}
	}
	if (isPlaceFree(p.getPosX() + 1, p.getPosY())) { //right
		if (exercises[1] == undefined) {
			do {
			exercises[1] = new Exercise(lvl);
			} while (answerOverlapping(1));
		}
	} else {
		if (exercises[1] !== undefined) {
			exercises[1] = undefined;
		}
	}
	if (isPlaceFree(p.getPosX(), p.getPosY() + 1)) { //down
		if (exercises[2] == undefined) {
			do {
			exercises[2] = new Exercise(lvl);
			} while (answerOverlapping(2));
		}
	} else {
		if (exercises[2] !== undefined) {
			exercises[2] = undefined;
		}
	}
	if (isPlaceFree(p.getPosX() - 1, p.getPosY())) { //left
		if (exercises[3] == undefined) {
			do {
			exercises[3] = new Exercise(lvl);
			} while (answerOverlapping(3));
		}
	} else {
		if (exercises[3] !== undefined) {
			exercises[3] = undefined;
		}
	}
}

function updateArrowKeys() {
	if (17 in keys && keys[17]) { //ctrl
		alert("CTRL");
		canvasLostFocus();
	}
	if (38 in keys && keys[38] || (87 in keys && keys[87])) { //up
		if (!p.isMoving()) {
			if (isPlaceFree(p.getPosX(), p.getPosY() - 1)) {
				p.moveY(-1);
			}
		}
	}
	if ((39 in keys && keys[39]) || (68 in keys && keys[68])) { //right
		if (!p.isMoving()) {
			if (isPlaceFree(p.getPosX() + 1, p.getPosY())) {
				p.moveX(1);
			}
		}
	}
	if ((40 in keys && keys[40]) || (83 in keys && keys[83])) { //down
		if (!p.isMoving()) {
			if (isPlaceFree(p.getPosX(), p.getPosY() + 1)) {
				p.moveY(1);
			}
		}
	}
	if (37 in keys && keys[37] || (65 in keys && keys[65])) { //left
		if (!p.isMoving()) {
			//alert(isPlaceFree(p.getPosX(), p.getPosY()));
			if (isPlaceFree(p.getPosX() - 1, p.getPosY())) {
				p.moveX(-1);
			}
		}
	}
}

function draw() {
	//ctx.drawImage(images["TilesSprite2"], 0, 0);
	drawWorld();
	p.draw();
	drawHud();
}

function drawWorld() {
	for (i = 0; i < 10; i++) {//level.map.lenght;
		for (j = 0; j < 10; j++) {
			var t = level.map[i][j];
			var tnum = parseInt(t.substr(1, 2));
			switch (t.charAt(0)) {
				case 'f':
				drawGridObject(images["TilesSprite2"], tnum, 1, i * grid, j * grid);
				break;
				case 'w':
				drawGridObject(images["TilesSprite2"], tnum, 2, i * grid, j * grid);
				break;
			}
		}
	}
}

function drawGridObject(spriteSheet,sx, sy, x, y) {
	try {  
    	ctx.drawImage(spriteSheet, sx * grid, sy * grid, grid, grid, 
    	x, y, grid, grid);
    } catch(e) {alert(e)}
}

function isPlaceFree(x, y) {
	var t = level.map[x][y];
	if (t.length > 0) {
		switch (t.charAt(0)) {
			case 'w':
			return false;
			break;
		}
	}
	return true;
}

function drawHud() {
	ctx.save();
	ctx.setTransform(1, 0, 0, 1, 0, 0);
	
	drawExercises();
	
	//FPS
	ctx.fillStyle = "#FFF";
	ctx.font = "bold 12px sans-serif";
  	ctx.fillText("fps: " + Math.min(curFPS,FPSlimit) + "/" + FPSlimit, 10, 20);
  	++numFramesDrawn;
	
	ctx.restore();
}

function drawExercises() {
	//draw Exercises
	if (inputCorrect < 0) {
		ctx.fillStyle = "rgba(255, 0, 0, 0.5)"; 
	}
	ctx.font = 'bold 30px sans-serif';
	ctx.lineWidth = 1;
    ctx.strokeStyle = "#000";
	var addWidth = 0;
    var text;
    var textW;
	for (i = 0; i < 4; i++) {
		if (exercises[i] !== undefined) {
			if (inputCorrect >= 0) {
				ctx.fillStyle = "rgba(255, 255, 255, 0.5)"; 
			}
			if (inputCorrect == i) {
				ctx.fillStyle = "rgba(255, 255, 255, 1)"; 
			}
			text = exercises[i].toString() + input;
	    	textW = ctx.measureText(text);
			switch (i) {
				case 0:
				ctx.fillText(text, canvas.width/2 - textW.width/2 - addWidth/2, canvas.height/2 - grid);
				ctx.strokeText(text, canvas.width/2 - textW.width/2 - addWidth/2, canvas.height/2 - grid);
				break;
				case 1:
				ctx.fillText(text, canvas.width/2 + grid, canvas.height/2 + 7);
				ctx.strokeText(text, canvas.width/2 + grid, canvas.height/2 + 7);
				break;
				case 2:
				ctx.fillText(text, canvas.width/2 - textW.width/2 - addWidth/2, canvas.height/2 + grid + 14);
				ctx.strokeText(text, canvas.width/2 - textW.width/2 - addWidth/2, canvas.height/2 + grid + 14);
				break;
				case 3:
				ctx.fillText(text, canvas.width/2 - textW.width - grid - addWidth, canvas.height/2 + 7);
				ctx.strokeText(text, canvas.width/2 - textW.width - grid - addWidth, canvas.height/2 + 7);
				break;
			}
		}
	}
}
	